package com.anysoft.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.anysoft.HostConstant;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.AddFeedbackBean;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.StringUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName：gy_fund-android
 * @ClassDescription：意见反馈
 * @CreateUser： yanbin
 * @CreateTime：2017-03-06 11:52
 * @ModifiedUser：
 * @ModifiedTime：
 * @ModifiedRemark：
 */

public class FeedBackActivity extends BaseActivity {
    @BindView(R.id.leftButton)
    Button leftButton;
    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.et_feedbakc_content)
    TextView et_feedbakc_content;
    @BindView(R.id.btn_feedback_submit)
    Button btn_feedback_submit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        title.setText("意见反馈");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @OnClick({R.id.leftButton, R.id.btn_feedback_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftButton:
                finish();
                break;
            case R.id.btn_feedback_submit:
                save();
                break;
        }
    }

    public void save() {

        String content = et_feedbakc_content.getText().toString();
        if (StringUtils.isNullOrEmpty(content)) {
            showConfirmAlert(mActivity, "请输入您的问题或建议");
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("fundCustomerNo", mentorApplication.userBean.getFundCustomerNo());
        params.put("identityNo", mentorApplication.accountLoginBean.getData().getIdentityNo());
        params.put("phoneNo", mentorApplication.accountLoginBean.getData().getMobileNo());
        params.put("name", mentorApplication.accountLoginBean.getData().getRealName());

        params.put("options", content);// 意见
//        params.put("identityNo", TextUtils.isEmpty(mentorApplication.accountLoginBean.getData().getIdentityNo()) ?
//                "" : mentorApplication.accountLoginBean.getData().getIdentityNo());// 证件号码
//        params.put("identityNo", TextUtils.isEmpty(mentorApplication.accountLoginBean.getData().getIdentityType()) ?
//                "" : mentorApplication.accountLoginBean.getData().getIdentityType());// 证件类型
//        params.put("identityNo", TextUtils.isEmpty(mentorApplication.accountLoginBean.getData().getMobileNo()) ?
//                "" : mentorApplication.accountLoginBean.getData().getMobileNo());// 电话号码
//        params.put("identityNo", TextUtils.isEmpty(mentorApplication.accountLoginBean.getData().getRealName()) ?
//                "" : mentorApplication.accountLoginBean.getData().getRealName());// 姓名
//        params.put("identityNo", TextUtils.isEmpty(mentorApplication.accountLoginBean.getData().getEmail()) ?
//                "" : mentorApplication.accountLoginBean.getData().getEmail());// 邮箱

        Logger.t("-----BASE_URL-----").d(HttpUtils.createRequestUrl(this, HostConstant.ADD_FEED_BACK, params));
        OkGo.<String>post(HostConstant.ADD_FEED_BACK)
                .tag(this)
                .params(HttpUtils.getCommonParams(this, params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(response.body());
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private void parseJson(String response) {
        AddFeedbackBean bean;

        try {
            bean = new Gson().fromJson(response, AddFeedbackBean.class);
        } catch (Exception e) {
            Logger.e(TAG, "Gson解析数据时发生异常");
            e.printStackTrace();
            return;
        }

        if (!CommonUtil.checkState(this, bean, response)) {
            return;
        }
        if (!StringUtils.isNullOrEmpty(bean.getData())) {
            showToast(bean.getData());
        } else {
            showToast("提交成功");
        }
        finish();
    }
}
